package com.zmn.oms.dubbo.impl.zmn.third.baidu;

import org.apache.dubbo.config.annotation.Service;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.dubbo.interfaces.zmn.third.baidu.BaiduApiRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.third.baidu_dtw.BaiduDtwConsts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiewenbing
 * @date:2020/11/24
 * description: 百度推广渠道H5页面接口实现
 */
@Slf4j
@Api(value = "百度推广渠道H5接口", tags = {"百度推广渠道H5接口"})
@Service(loadbalance = "roundrobin", timeout = 10000, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class BaiduApiRemoteServiceImpl implements BaiduApiRemoteService {

    protected static String BAIDU_API_BASE_URL="https://test2-api-oms.xiujiadian.com/baidu/api/newOrder?channelId=%d";

    protected static Map<Integer,String> OLD_CHANNEL_URL_MAP = new HashMap<>(17);
    {
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_3,"https://api-oms.xiujiadian.com/baidu/dtw/order_3");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_11,"https://api-oms.xiujiadian.com/baidu/dtw/order_11");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_32,"https://api-oms.xiujiadian.com/baidu/dtw/order_32");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_101,"https://api-oms.xiujiadian.com/baidu/dtw/order_101");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_102,"https://api-oms.xiujiadian.com/baidu/dtw/order_102");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_103,"https://api-oms.xiujiadian.com/baidu/dtw/order_103");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_104,"https://api-oms.xiujiadian.com/baidu/dtw/order_104");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_105,"https://api-oms.xiujiadian.com/baidu/dtw/order_105");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_106,"https://api-oms.xiujiadian.com/baidu/dtw/order_106");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_107,"https://api-oms.xiujiadian.com/baidu/dtw/order_107");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_108,"https://api-oms.xiujiadian.com/baidu/dtw/order_108");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_109,"https://api-oms.xiujiadian.com/baidu/dtw/order_109");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_110,"https://api-oms.xiujiadian.com/baidu/dtw/order_110");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_111,"https://api-oms.xiujiadian.com/baidu/dtw/order_111");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_112,"https://api-oms.xiujiadian.com/baidu/dtw/order_112");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_113,"https://api-oms.xiujiadian.com/baidu/dtw/order_113");
        OLD_CHANNEL_URL_MAP.put(BaiduDtwConsts.CHANNEL_ID_114,"https://api-oms.xiujiadian.com/baidu/dtw/order_114");
    }
    /**
     * 获取渠道对接的接口URL
     *
     * @param channelId
     * @return
     */
    @Override
    @ApiOperation(value = "查询渠道对应的URL")
    public ResponseDTO<String> getChannelAPIUrl(@Valid @NotNull(message = "渠道id不能为空") Integer channelId) {

        if (NumberUtil.isNullOrZero(channelId)) {
            return ResponseDTO.fail("渠道id不能为0");
        }

        String channelApiUrl = OLD_CHANNEL_URL_MAP.get(channelId);
        if (StringUtil.isBlank(channelApiUrl)) {
            channelApiUrl = String.format(BaiduApiRemoteServiceImpl.BAIDU_API_BASE_URL, channelId);
        }

        return ResponseDTO.success(channelApiUrl,"success");
    }

    @NacosValue(value="${baidu.api.service.url:https://test2-api-oms.xiujiadian.com/baidu/api/newOrder?channelId=%d}", autoRefreshed = true)
    public void setBaiduApiBaseUrl(String url) {
        BAIDU_API_BASE_URL = url;
    }
}
